local assets = {
	Asset("ANIM", "anim/musha_glowdust.zip"),
	Asset("ATLAS", "images/inventoryimages/musha_glowdust.xml"),
	Asset("IMAGE", "images/inventoryimages/musha_glowdust.tex")
}

local function OnHaunt(inst)
	inst.components.disappears:Disappear()
	return true
end

local function create_light(eater, lightprefab)
	if eater.wormlight ~= nil then
		if eater.wormlight.prefab == lightprefab then
			eater.wormlight.components.spell.lifetime = 0
			eater.wormlight.components.spell:ResumeSpell()
			return
		else
			eater.wormlight.components.spell:OnFinish()
		end
	end

	local light = SpawnPrefab(lightprefab)
	light.components.spell:SetTarget(eater)
	if light:IsValid() then
		if light.components.spell.target == nil then
			light:Remove()
		else
			light.components.spell:StartSpell()
		end
	end
end

local function item_oneaten(inst, eater)
	if eater.components.musha_staminasleep then
		eater.components.musha_fatiguesleep:DoDelta(-20)
		eater.components.musha_staminasleep:DoDelta(10)
	end
	if eater.components.musha_spellpower then
		eater.components.musha_spellpower:DoDelta(20)
	end

	create_light(eater, "wormlight_light")
end

local function fn()
	local inst = CreateEntity()
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()
	inst.entity:AddLight()

	MakeInventoryPhysics(inst)
	MakeInventoryFloatable(inst, nil, nil)
	MakeInventoryPhysics(inst)

	inst.AnimState:SetBank("ashes")
	inst.AnimState:SetBuild("musha_glowdust")
	inst.AnimState:PlayAnimation("idle")

	inst.Light:SetFalloff(0.7)
	inst.Light:SetIntensity(.5)
	inst.Light:SetRadius(0.5)
	inst.Light:SetColour(69 / 255, 131 / 255, 145 / 255)
	inst.Light:Enable(true)
	inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
	inst.Transform:SetScale(0.7, 0.7, 0.7)

	inst.entity:SetPristine()

	if not TheWorld.ismastersim then
		return inst
	end

	inst:AddComponent("inspectable")
	inst:AddComponent("fuel")
	inst.components.fuel.fuelvalue = TUNING.LARGE_FUEL
	inst.components.fuel.fueltype = "CAVE"
	inst:AddComponent("disappears")
	inst.components.disappears.sound = "dontstarve/common/dust_blowaway"
	inst.components.disappears.anim = "disappear"
	inst:AddComponent("stackable")
	inst.components.stackable.maxsize = TUNING.STACK_SIZE_SMALLITEM
	inst:AddComponent("inventoryitem")
	inst.components.inventoryitem:SetOnPutInInventoryFn(
		function()
			inst.components.disappears:StopDisappear()
		end
	)
	inst.components.inventoryitem.atlasname = "images/inventoryimages/musha_glowdust.xml"

	inst:AddComponent("tradable")
	inst.components.tradable.goldvalue = 7

	inst:AddComponent("edible")
	inst.components.edible.foodtype = FOODTYPE.GOODIES --FOODTYPE.VEGGIE
	inst.components.edible.healthvalue = 10
	inst.components.edible.hungervalue = 5
	inst.components.edible.sanityvalue = 30
	inst.components.edible:SetOnEatenFn(item_oneaten)

	inst:ListenForEvent(
		"ondropped",
		function()
			inst.components.disappears:PrepareDisappear()
		end
	)
	inst.components.disappears:StopDisappear()

	inst:AddComponent("hauntable")
	inst.components.hauntable.cooldown_on_successful_haunt = false
	inst.components.hauntable.usefx = false
	inst.components.hauntable:SetHauntValue(TUNING.HAUNT_TINY)
	inst.components.hauntable:SetOnHauntFn(OnHaunt)

	return inst
end

return Prefab("musha_glowdust", fn, assets)
